package com.shop.front.service.user;

import com.alibaba.fastjson.JSON;
import com.shop.common.enums.TrueFalseEnum;
import com.shop.domain.user.UserBankCard;
import com.shop.front.dao.user.UserBankCardDao;
import com.shop.front.dao.user.UserBankCardSQLDao;
import com.shop.front.request.user.UserBankCardReq;
import com.shop.util.BeanCopyUtils;
import com.shop.web.context.WebContext;
import com.shop.web.response.BaseResponse;
import lombok.extern.slf4j.Slf4j;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.data.domain.Page;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;

import java.util.List;

@Service
@Slf4j
@Transactional
public class UserBankCardService {

    private static final Logger specialLog = LoggerFactory.getLogger("sys.special.log");

    @Autowired
    private UserBankCardDao userBankCardDao;
    @Autowired
    private UserBankCardSQLDao userBankCardSQLDao;

    public BaseResponse findSingle(Long id) {
        UserBankCard userBankCard = userBankCardDao.findSingle(id);
        return new BaseResponse(userBankCard);
    }

    public BaseResponse findDefault(Long userId,String cardType) {
        UserBankCard userBankCard = userBankCardDao.findDefault(userId,cardType);
        if(null != userBankCard) {
            return new BaseResponse(userBankCard);
        }

        List<UserBankCard> list = userBankCardDao.findByUid(userId,cardType);
        if(null != list && list.size() > 0) {
            return new BaseResponse(list.get(0));
        }

        return new BaseResponse();
    }

    public BaseResponse page(Long userId,String cardType) {
        Page<UserBankCard> page = userBankCardSQLDao.page(userId,cardType);
        return new BaseResponse(page);
    }

    public BaseResponse add(Long userId, UserBankCardReq req) {
        UserBankCard userBankCard = new UserBankCard();

        try {
            // 字段赋值
            BeanCopyUtils.copyProperties(userBankCard, req);
        } catch (Exception e) {
            log.error("添加用户银行卡赋值异常！", e);
            return BaseResponse.FAIL;
        }

        // 是否设置为默认
        if(TrueFalseEnum.TRUE.getCode() == req.getIsDefault()) {    // 是
            // 修改该用户默认收货地址为非默认
            userBankCardDao.updateNotDefault(userId);
        }

        // 保存入库
        userBankCard.setUid(userId);
        userBankCardDao.save(userBankCard);

        return new BaseResponse(userBankCard);
    }

    public BaseResponse edit(Long id,Long userId, UserBankCardReq req) {
        UserBankCard userBankCard = userBankCardDao.findSingle(id);
        if(null == userBankCard) {
            return BaseResponse.fail("该银行卡信息不存在!");
        }
        if(!userBankCard.getUid().equals(userId)){
            specialLog.info("~~~进入异常串值判断！~~~");
            specialLog.info("当前用户id -> {}", userId);
            specialLog.info("当前请求的用户信息 -> {}", JSON.toJSONString(WebContext.getPrincipal()));
            specialLog.info("当前请求request参数 -> {}", JSON.toJSONString(req));
            specialLog.info("当前获取到的对象信息 -> {}", JSON.toJSONString(userBankCard));
            specialLog.info("当前请求的IP地址 -> {}", WebContext.getRequestIp());
            return BaseResponse.fail("操作失败，请稍后重试！");
        }

        // 是否设置为默认
        if(TrueFalseEnum.TRUE.getCode() == req.getIsDefault()) {    // 是
            // 当前是否为非默认
            if(TrueFalseEnum.TRUE.getCode() != userBankCard.getIsDefault()) {
                // 修改该用户默认收货地址为非默认
                userBankCardDao.updateNotDefault(userBankCard.getUid());
            }
        }

        try {
            // 字段赋值
            BeanCopyUtils.copyProperties(userBankCard, req);
        } catch (Exception e) {
            log.error("添加用户地址赋值异常！", e);
            return BaseResponse.FAIL;
        }

        // 保存入库
        userBankCardDao.save(userBankCard);

        return new BaseResponse(userBankCard);
    }

    public BaseResponse del(Long id,Long userId) {
        UserBankCard userBankCard = userBankCardDao.findSingle(id);
        if(null == userBankCard) {
            return BaseResponse.fail("该银行卡信息不存在!");
        }

        if(!userBankCard.getUid().equals(userId)){
            specialLog.info("~~~进入异常串值判断！~~~");
            specialLog.info("当前用户id -> {}", userId);
            specialLog.info("当前请求的用户信息 -> {}", JSON.toJSONString(WebContext.getPrincipal()));
            specialLog.info("当前请求request参数 -> {}", id);
            specialLog.info("当前获取到的对象信息 -> {}", JSON.toJSONString(userBankCard));
            specialLog.info("当前请求的IP地址 -> {}", WebContext.getRequestIp());
            return BaseResponse.fail("操作失败，请稍后重试！");
        }

        // 删除
        userBankCardDao.delete(userBankCard);

        return new BaseResponse();
    }
}
